home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / util / pack / xfdmaster.lha / xfd / Developer / Sources / C / GZip.c < prev    next >
C/C++ Source or Header  |  1999-02-05  |  25KB  |  872 lines

  1. unsigned char version[] = "$VER: GZip 1.1 (07.12.1998) by SDI";
  2.  
  3. /* Objectheader
  4.  
  5.     Name:        GZip.c
  6.     Description:    xfd external decruncher for GZip files
  7.     Author:     SDI (stoecker@amigaworld.com)
  8.     Distribution:    PD
  9.  
  10.  1.0   01.11.98 : first version
  11.  1.1   07.12.98 : added buffer checking, reduced stack usage a lot
  12. */
  13.  
  14. /* I made that client with files gzip.c, gzip.h, util.c, unzip.c and
  15.    inflate.c of gzip source distribution. I changed the client to do
  16.    mem to mem decrunching and generally made it an Amiga style source
  17.    code (variable types, no global variables, AllocVec, ...). All
  18.    the register variables got normal ones, as SAS should know better
  19.    which variables should be in registers. The CRC table has been
  20.    replaced by a CRC table calculation.
  21. */ 
  22.  
  23. #include <libraries/xfdmaster.h>
  24. #include <proto/exec.h>
  25. #include <exec/memory.h>
  26. #include <exec/execbase.h>
  27.  
  28. /* gzip flag byte */
  29. #define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
  30. #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
  31. #define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
  32. #define ORIG_NAME    0x08 /* bit 3 set: original file name present */
  33. #define COMMENT      0x10 /* bit 4 set: file comment present */
  34. #define ENCRYPTED    0x20 /* bit 5 set: file is encrypted */
  35. #define RESERVED     0xC0 /* bit 6,7:    reserved */
  36.  
  37. /* If BMAX needs to be larger than 16, then h and x[] should be ULONG. */
  38. #define BMAX 16     /* maximum bit length of any code (16 for explode) */
  39. #define N_MAX 288    /* maximum number of codes in any set */
  40.  
  41. struct GZipData {
  42.   struct ExecBase *    SysBase;
  43.   UBYTE *        outbuf;
  44.   UBYTE *        inbuf;
  45.   ULONG         outsize;
  46.   ULONG         insize;
  47.   ULONG         crc;
  48.   ULONG         XFDerr;
  49.  
  50.   ULONG         outpos;
  51.   ULONG         inpos;
  52.   ULONG         bb;
  53.   ULONG         bk;
  54.  
  55.   ULONG            data1[320]; /* all functions */
  56.  
  57.   /* huft_build */
  58.   ULONG            c[BMAX+1];        /* bit length count table */
  59.   struct huft *        u[BMAX];        /* table stack */
  60.   ULONG         v[N_MAX];        /* values in order of bit length */
  61.   ULONG         x[BMAX+1];        /* bit offsets, then code stack */
  62. };
  63.  
  64. #define BUFFEREXTEND    100
  65.  
  66. /************** Here starts xfd stuff - the xfdSlave structure **********/
  67.  
  68. #define MASTER_VERS    36
  69.  
  70. typedef BOOL (*xfdFunc) ();
  71.  
  72. LONG __asm RecogGZip(register __a0 STRPTR buf, register __d0 ULONG length);
  73. BOOL __asm DecrunchGZip(register __a0 struct xfdBufferInfo * xbi,
  74. register __a6 struct xfdMasterBase *xfdMasterBase);
  75. BOOL decrunchZip(struct GZipData *);
  76. LONG huft_build(ULONG *, ULONG, ULONG, UWORD *, UWORD *,
  77. struct huft **, LONG *, struct GZipData *g);
  78. void huft_free(struct huft *, struct GZipData *g);
  79. LONG inflate_codes(struct huft *, struct huft *, LONG, LONG,
  80. struct GZipData *g);
  81. LONG inflate_stored(struct GZipData *g);
  82. LONG inflate_fixed(struct GZipData *g);
  83. LONG inflate_dynamic(struct GZipData *g);
  84.  
  85. struct xfdSlave FirstSlave = {
  86.   0, XFDS_VERSION, MASTER_VERS, "GZip", XFDPFF_DATA, 0, 
  87.   (xfdFunc) RecogGZip, (xfdFunc) DecrunchGZip, 0, 0, 0, 0, 4};
  88.  
  89. LONG __asm RecogGZip(register __a0 STRPTR buf,
  90. register __d0 ULONG length)
  91. {
  92.   return (*(buf++) == 0x1F && (*buf == 0x8B || *buf == 0x9E) && buf[1] == 8
  93.   && !(buf[2] & (ENCRYPTED|CONTINUATION|RESERVED)));
  94. }
  95.  
  96. BOOL __asm DecrunchGZip(register __a0 struct xfdBufferInfo * xbi,
  97. register __a6 struct xfdMasterBase *xfdmasterBase)
  98. {
  99.   STRPTR inbuf, outbuf;
  100.   ULONG flags, outsize;
  101.   BOOL res = 0;
  102.   struct ExecBase *SysBase;
  103.   struct GZipData *g;
  104.  
  105.   SysBase = xfdmasterBase->xfdm_ExecBase;
  106.   inbuf = (STRPTR) xbi->xfdbi_SourceBuffer;
  107.   /* We need OS2.0 for AllocVec */
  108.   if(SysBase->LibNode.lib_Version < 36)
  109.   {
  110.     xbi->xfdbi_Error = XFDERR_NOTSUPPORTED;
  111.     return 0;
  112.   }
  113.  
  114.   outsize = (*(inbuf + xbi->xfdbi_SourceBufLen-1)<<24) +
  115.         (*(inbuf + xbi->xfdbi_SourceBufLen-2)<<16) +
  116.         (*(inbuf + xbi->xfdbi_SourceBufLen-3)<<8)  +
  117.         *(inbuf + xbi->xfdbi_SourceBufLen-4) + BUFFEREXTEND;
  118.  
  119.   inbuf += 3;            /* skip header */
  120.   flags = *(inbuf++);
  121.   inbuf += 6;            /* skip time, extra flags, OS type */
  122.  
  123.   if(flags & EXTRA_FIELD)    /* skip extra field */
  124.     inbuf += 2 + *inbuf + ((*(inbuf+1))<<8);
  125.  
  126.   if(flags & ORIG_NAME)     /* skip file name */
  127.   {
  128.     while(*(inbuf++))
  129.       ;
  130.   }
  131.  
  132.   if(flags & COMMENT)
  133.   {
  134.     while(*(inbuf++))
  135.       ;
  136.   }
  137.  
  138.   if((g = (struct GZipData *) AllocMem(sizeof(struct GZipData), MEMF_CLEAR)))
  139.   {
  140.     g->XFDerr = XFDERR_NOMEMORY;
  141.  
  142.     if((outbuf = (STRPTR) AllocMem(outsize, xbi->xfdbi_TargetBufMemType)))
  143.     {
  144.       g->SysBase = SysBase;
  145.       g->outbuf = outbuf;
  146.       g->inbuf = inbuf;
  147.       g->outsize = outsize;
  148.       g->insize = xbi->xfdbi_SourceBufLen-(inbuf-
  149.            (STRPTR)xbi->xfdbi_SourceBuffer)-8;
  150.       g->crc = (inbuf[g->insize+3]<<24)+(inbuf[g->insize+2]<<16)+
  151.         (inbuf[g->insize+1]<<8)+inbuf[g->insize];
  152.  
  153.       res = decrunchZip(g);
  154.  
  155.       if(res)
  156.       {
  157.         xbi->xfdbi_TargetBuffer = outbuf;
  158.         xbi->xfdbi_TargetBufLen = outsize;
  159.         xbi->xfdbi_TargetBufSaveLen = outsize - BUFFEREXTEND;
  160.       }
  161.       else
  162.       {
  163.         FreeMem(outbuf, outsize);
  164.         xbi->xfdbi_Error = g->XFDerr;
  165.       }
  166.     }
  167.     else
  168.       xbi->xfdbi_Error = XFDERR_NOMEMORY;
  169.  
  170.     FreeMem(g, sizeof(struct GZipData));
  171.   }
  172.   else
  173.     xbi->xfdbi_Error = XFDERR_NOMEMORY;
  174.  
  175.  
  176.   return res;
  177. }
  178.  
  179. #define SysBase g->SysBase
  180.  
  181. /* Huffman code lookup table entry--this entry is four bytes for machines
  182.    that have 16-bit pointers (e.g. PC's in the small or medium model).
  183.    Valid extra bits are 0..13.    e == 15 is EOB (end of block), e == 16
  184.    means that v is a literal, 16 < e < 32 means that v is a pointer to
  185.    the next table, which codes e - 16 bits, and lastly e == 99 indicates
  186.    an unused code.  If a code with e == 99 is looked up, this implies an
  187.    error in the data. */
  188. struct huft {
  189.   UBYTE e;        /* number of extra bits or operation */
  190.   UBYTE b;        /* number of bits in this code or subcode */
  191.   union {
  192.     UWORD n;        /* literal, length base, or distance base */
  193.     struct huft *t;    /* pointer to next level of table */
  194.   } v;
  195. };
  196.  
  197. /* Tables for deflate from PKZIP's appnote.txt. */
  198. static ULONG border[] = {    /* Order of the bit length code lengths */
  199.   16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
  200. static UWORD cplens[] = {    /* Copy lengths for literal codes 257..285 */
  201.   3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  202.   35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
  203. static UWORD cplext[] = {    /* Extra bits for literal codes 257..285 */
  204.   0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  205.   3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
  206. static UWORD cpdist[] = {    /* Copy offsets for distance codes 0..29 */
  207.   1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  208.   257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  209.   8193, 12289, 16385, 24577};
  210. static UWORD cpdext[] = {    /* Extra bits for distance codes */
  211.   0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  212.   7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
  213.  
  214. /* Macros for inflate() bit peeking and grabbing.
  215.    The usage is:
  216.    
  217.     NEEDBITS(j)
  218.     x = b & mask_bits[j];
  219.     DUMPBITS(j)
  220.  
  221.    where NEEDBITS makes sure that b has at least j bits in it, and
  222.    DUMPBITS removes the bits from b.  The macros use the variable k
  223.    for the number of bits in b.  Normally, b and k are register
  224.    variables for speed, and are initialized at the beginning of a
  225.    routine that uses these macros from a global bit buffer and count.
  226.  
  227.    If we assume that EOB will be the longest code, then we will never
  228.    ask for bits with NEEDBITS that are beyond the end of the stream.
  229.    So, NEEDBITS should not read any more bytes than are needed to
  230.    meet the request.  Then no bytes need to be "returned" to the buffer
  231.    at the end of the last block.
  232.  
  233.    However, this assumption is not true for fixed blocks--the EOB code
  234.    is 7 bits, but the other literal/length codes can be 8 or 9 bits.
  235.    (The EOB code is shorter than other codes because fixed blocks are
  236.    generally short.  So, while a block always has an EOB, many other
  237.    literal/length codes have a significantly lower probability of
  238.    showing up at all.)    However, by making the first table have a
  239.    lookup of seven bits, the EOB code will be found in that first
  240.    lookup, and so will not require that too many bits be pulled from
  241.    the stream.
  242.  */
  243.  
  244. UWORD mask_bits[] = {
  245.     0x0000,
  246.     0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
  247.     0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
  248. };
  249.  
  250. #define NEXTBYTE()  (g->inbuf[g->inpos++])
  251. #define NEEDBITS(n) {while(k<(n)){b|=((ULONG)NEXTBYTE())<<k;k+=8;}}
  252. #define DUMPBITS(n) {b>>=(n);k-=(n);}
  253.  
  254. /*
  255.    Huffman code decoding is performed using a multi-level table lookup.
  256.    The fastest way to decode is to simply build a lookup table whose
  257.    size is determined by the longest code.  However, the time it takes
  258.    to build this table can also be a factor if the data being decoded
  259.    is not very long.  The most common codes are necessarily the
  260.    shortest codes, so those codes dominate the decoding time, and hence
  261.    the speed.  The idea is you can have a shorter table that decodes the
  262.    shorter, more probable codes, and then point to subsidiary tables for
  263.    the longer codes.  The time it costs to decode the longer codes is
  264.    then traded against the time it takes to make longer tables.
  265.  
  266.    This results of this trade are in the variables lbits and dbits
  267.    below.  lbits is the number of bits the first level table for literal/
  268.    length codes can decode in one step, and dbits is the same thing for
  269.    the distance codes.    Subsequent tables are also less than or equal to
  270.    those sizes.  These values may be adjusted either when all of the
  271.    codes are shorter than that, in which case the longest code length in
  272.    bits is used, or when the shortest code is *longer* than the requested
  273.    table size, in which case the length of the shortest code in bits is
  274.    used.
  275.  
  276.    There are two different values for the two tables, since they code a
  277.    different number of possibilities each.  The literal/length table
  278.    codes 286 possible values, or in a flat code, a little over eight
  279.    bits.  The distance table codes 30 possible values, or a little less
  280.    than five bits, flat.  The optimum values for speed end up being
  281.    about one bit more than those, so lbits is 8+1 and dbits is 5+1.
  282.    The optimum values may differ though from machine to machine, and
  283.    possibly even between compilers.  Your mileage may vary.
  284.  */
  285.  
  286. #define lbits 9;    /* bits in base literal/length lookup table */
  287. #define dbits 6;    /* bits in base distance lookup table */
  288.  
  289. LONG huft_build(ULONG *b, ULONG n, ULONG s, UWORD *d, UWORD *e, 
  290. struct huft **t, LONG *m, struct GZipData *g)
  291. /* b - code lengths in bits (all assumed <= BMAX) */
  292. /* n - number of codes (assumed <= N_MAX) */
  293. /* s - number of simple-valued codes (0..s-1) */
  294. /* d - list of base values for non-simple codes */
  295. /* e - list of extra bits for non-simple codes */
  296. /* t - result: starting table */
  297. /* m - maximum lookup bits, returns actual */
  298. /* Given a list of code lengths and a maximum table size, make a set of
  299.    tables to decode that set of codes.    Return zero on success, one if
  300.    the given code set is incomplete (the tables are still built in this
  301.    case), two if the input is invalid (all zero length codes or an
  302.    oversubscribed set of lengths), and three if not enough memory. */
  303. {
  304.   ULONG a;        /* counter for codes of length k */
  305.   ULONG f;        /* i repeats in table every f entries */
  306.   LONG g2;        /* maximum code length */
  307.   LONG h;        /* table level */
  308.   ULONG i;        /* counter, current code */
  309.   ULONG j;        /* counter */
  310.   LONG k;        /* number of bits in current code */
  311.   LONG l;        /* bits per table (returned in m) */
  312.   ULONG *p;        /* pointer into c[], b[], or v[] */
  313.   struct huft *q;    /* points to current table */
  314.   struct huft r;    /* table entry for structure assignment */
  315.   LONG w;        /* bits before this table == (l * h) */
  316.   ULONG *xp;        /* pointer into x */
  317.   LONG y;        /* number of dummy codes added */
  318.   ULONG z;        /* number of entries in current table */
  319.  
  320.   /* Generate counts for each bit length */
  321.   for(i = 0; i < BMAX+1; ++i)
  322.     g->c[i] = 0;
  323.  
  324.   p = b; i = n;
  325.   do {
  326.     g->c[*p++]++;            /* assume all entries <= BMAX */
  327.   } while (--i);
  328.   if (g->c[0] == n)        /* null input--all zero length codes */
  329.   {
  330.     *t = (struct huft *)NULL;
  331.     *m = 0;
  332.     return 0;
  333.   }
  334.  
  335.   /* Find minimum and maximum length, bound *m by those */
  336.   l = *m;
  337.   for (j = 1; j <= BMAX; j++)
  338.     if (g->c[j])
  339.       break;
  340.   k = j;            /* minimum code length */
  341.   if ((ULONG)l < j)
  342.     l = j;
  343.   for (i = BMAX; i; i--)
  344.     if (g->c[i])
  345.       break;
  346.   g2 = i;             /* maximum code length */
  347.   if ((ULONG)l > i)
  348.     l = i;
  349.   *m = l;
  350.  
  351.  
  352.   /* Adjust last length count to fill out codes, if needed */
  353.   for (y = 1 << j; j < i; j++, y <<= 1)
  354.     if((y -= g->c[j]) < 0)
  355.       return 2;         /* bad input: more codes than bits */
  356.   if ((y -= g->c[i]) < 0)
  357.     return 2;
  358.   g->c[i] += y;
  359.  
  360.  
  361.   /* Generate starting offsets into the value table for each length */
  362.   g->x[1] = j = 0;
  363.   p = g->c + 1;  xp = g->x + 2;
  364.   while (--i) {         /* note that i == g from above */
  365.     *xp++ = (j += *p++);
  366.   }
  367.  
  368.  
  369.   /* Make a table of values in order of bit lengths */
  370.   p = b;  i = 0;
  371.   do {
  372.     if ((j = *p++) != 0)
  373.       g->v[g->x[j]++] = i;
  374.   } while (++i < n);
  375.  
  376.  
  377.   /* Generate the Huffman codes and for each, make the table entries */
  378.   g->x[0] = i = 0;         /* first Huffman code is zero */
  379.   p = g->v;            /* grab values in bit order */
  380.   h = -1;            /* no tables yet--level -1 */
  381.   w = -l;            /* bits decoded == (l * h) */
  382.   g->u[0] = (struct huft *)NULL;    /* just to keep compilers happy */
  383.   q = (struct huft *)NULL;    /* ditto */
  384.   z = 0;            /* ditto */
  385.  
  386.   /* go through the bit lengths (k already is bits in shortest code) */
  387.   for (; k <= g2; k++)
  388.   {
  389.     a = g->c[k];
  390.     while (a--)
  391.     {
  392.       /* here i is the Huffman code of length k bits for value *p */
  393.       /* make tables up to required level */
  394.       while (k > w + l)
  395.       {
  396.     h++;
  397.     w += l;         /* previous table always l bits */
  398.  
  399.     /* compute minimum size table less than or equal to l bits */
  400.     z = (z = g2 - w) > (ULONG)l ? l : z;  /* upper limit on table size */
  401.     if ((f = 1 << (j = k - w)) > a + 1)    /* try a k-w bit table */
  402.     {            /* too few codes for k-w bit table */
  403.       f -= a + 1;        /* deduct codes from patterns left */
  404.       xp = g->c + k;
  405.       while (++j < z)    /* try smaller tables up to z bits */
  406.       {
  407.         if ((f <<= 1) <= *++xp)
  408.           break;        /* enough codes to use up j bits */
  409.         f -= *xp;        /* else deduct codes from patterns */
  410.       }
  411.     }
  412.     z = 1 << j;        /* table entries for j-bit table */
  413.  
  414.     /* allocate and link in new table */
  415.     if(!(q = (struct huft *)AllocVec((z + 1)*sizeof(struct huft), MEMF_CLEAR)))
  416.     {
  417.       if(h)
  418.         huft_free(g->u[0],g);
  419.       return 100;        /* not enough memory */
  420.     }
  421.     *t = q + 1;        /* link to list for huft_free() */
  422.     *(t = &(q->v.t)) = (struct huft *)NULL;
  423.     g->u[h] = ++q;        /* table starts after link */
  424.  
  425.     /* connect to last table, if there is one */
  426.     if (h)
  427.     {
  428.       g->x[h] = i;        /* save pattern for backing up */
  429.       r.b = (UBYTE)l;    /* bits to dump before this table */
  430.       r.e = (UBYTE)(16 + j); /* bits in this table */
  431.       r.v.t = q;        /* pointer to this table */
  432.       j = i >> (w - l);    /* (get around Turbo C bug) */
  433.       g->u[h-1][j] = r;    /* connect to last table */
  434.     }
  435.       }
  436.  
  437.       /* set up table entry in r */
  438.       r.b = (UBYTE)(k - w);
  439.       if (p >= g->v + n)
  440.     r.e = 99;        /* out of values--invalid code */
  441.       else if (*p < s)
  442.       {
  443.     r.e = (UBYTE)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */
  444.     r.v.n = (UWORD)(*p);    /* simple code is just the value */
  445.     p++;            /* one compiler does not like *p++ */
  446.       }
  447.       else
  448.       {
  449.     r.e = (UBYTE)e[*p - s]; /* non-simple--look up in lists */
  450.     r.v.n = d[*p++ - s];
  451.       }
  452.  
  453.       /* fill code-like entries with r */
  454.       f = 1 << (k - w);
  455.       for (j = i >> w; j < z; j += f)
  456.     q[j] = r;
  457.  
  458.       /* backwards increment the k-bit code i */
  459.       for (j = 1 << (k - 1); i & j; j >>= 1)
  460.     i ^= j;
  461.       i ^= j;
  462.  
  463.       /* backup over finished tables */
  464.       while ((i & ((1 << w) - 1)) != g->x[h])
  465.       {
  466.     h--;            /* don't need to update q */
  467.     w -= l;
  468.       }
  469.     }
  470.   }
  471.  
  472.   /* Return true (1) if we were given an incomplete table */
  473.   return y != 0 && g2 != 1;
  474. }
  475.  
  476. void huft_free(struct huft *t, struct GZipData *g) /* table to free */
  477. /* Free the allocated tables built by huft_build(), which makes a linked
  478.    list of the tables it made, with the links in a dummy first entry of
  479.    each table. */
  480. {
  481.   struct huft *q;
  482.  
  483.   while(t)
  484.   {
  485.     q = (--t)->v.t;
  486.     FreeVec(t);
  487.     t = q;
  488.   } 
  489. }
  490.  
  491. LONG inflate_codes(struct huft *tl, struct huft *td, LONG bl, LONG bd,
  492. struct GZipData *g)
  493. /* tl, td -literal/length and distance decoder tables */
  494. /* bl, bd -number of bits decoded by tl[] and td[] */
  495. /* inflate (decompress) the codes in a deflated (compressed) block.
  496.    Return an error code or zero if it all goes ok. */
  497. {
  498.   ULONG e;        /* table entry flag/number of extra bits */
  499.   ULONG n, d;        /* length and index for copy */
  500.   struct huft *t;    /* pointer to table entry */
  501.   ULONG ml, md;     /* masks for bl and bd bits */
  502.   ULONG b;        /* bit buffer */
  503.   ULONG k;        /* number of bits in bit buffer */
  504.  
  505.   /* make local copies of globals */
  506.   b = g->bb;        /* initialize bit buffer */
  507.   k = g->bk;
  508.  
  509.   /* inflate the coded data */
  510.   ml = mask_bits[bl];        /* precompute masks for speed */
  511.   md = mask_bits[bd];
  512.   for(;;)            /* do until end of block */
  513.   {
  514.     NEEDBITS((ULONG)bl)
  515.     if ((e = (t = tl + ((ULONG)b & ml))->e) > 16)
  516.       do {
  517.     if (e == 99)
  518.       return 1;
  519.     DUMPBITS(t->b)
  520.     e -= 16;
  521.     NEEDBITS(e)
  522.       } while ((e = (t = t->v.t + ((ULONG)b & mask_bits[e]))->e) > 16);
  523.     DUMPBITS(t->b)
  524.     if(e == 16)         /* then it's a literal */
  525.     {
  526.       if(g->outpos >= g->outsize)
  527.         return 300;
  528.       g->outbuf[g->outpos++] = (UBYTE)t->v.n;
  529.     }
  530.     else            /* it's an EOB or a length */
  531.     {
  532.       /* exit if end of block */
  533.       if (e == 15)
  534.     break;
  535.  
  536.       /* get length of block to copy */
  537.       NEEDBITS(e)
  538.       n = t->v.n + ((ULONG)b & mask_bits[e]);
  539.       DUMPBITS(e);
  540.  
  541.       /* decode distance of block to copy */
  542.       NEEDBITS((ULONG)bd)
  543.       if ((e = (t = td + ((ULONG)b & md))->e) > 16)
  544.     do {
  545.       if (e == 99)
  546.         return 1;
  547.       DUMPBITS(t->b)
  548.       e -= 16;
  549.       NEEDBITS(e)
  550.     } while ((e = (t = t->v.t + ((ULONG)b & mask_bits[e]))->e) > 16);
  551.       DUMPBITS(t->b)
  552.       NEEDBITS(e)
  553.       d = g->outpos - t->v.n - ((ULONG)b & mask_bits[e]);
  554.       DUMPBITS(e)
  555.  
  556.       /* do the copy */
  557.       if(g->outpos + n >= g->outsize)
  558.         return 300;
  559.       do {
  560.     g->outbuf[g->outpos++] = g->outbuf[d++];
  561.       } while(--n);
  562.     }
  563.   }
  564.  
  565.   /* restore the globals from the locals */
  566.   g->bb = b;            /* restore global bit buffer */
  567.   g->bk = k;
  568.  
  569.   /* done */
  570.   return 0;
  571. }
  572.  
  573. LONG inflate_stored(struct GZipData *g)
  574. /* "decompress" an inflated type 0 (stored) block. */
  575. {
  576.   ULONG n;    /* number of bytes in block */
  577.   ULONG b;    /* bit buffer */
  578.   ULONG k;    /* number of bits in bit buffer */
  579.  
  580.   /* make local copies of globals */
  581.   b = g->bb;            /* initialize bit buffer */
  582.   k = g->bk;
  583.  
  584.   /* go to byte boundary */
  585.   n = k & 7;
  586.   DUMPBITS(n);
  587.  
  588.   /* get the length and its complement */
  589.   NEEDBITS(16)
  590.   n = ((ULONG)b & 0xffff);
  591.   DUMPBITS(16)
  592.   NEEDBITS(16)
  593.   if(n != (ULONG)((~b) & 0xffff))
  594.     return 1;            /* error in compressed data */
  595.   DUMPBITS(16)
  596.  
  597.   /* read and output the compressed data */
  598.   if(g->outpos + n >= g->outsize)
  599.     return 300;
  600.   while (n--)
  601.   {
  602.     NEEDBITS(8)
  603.     g->outbuf[g->outpos++] = (UBYTE)b;
  604.     DUMPBITS(8)
  605.   }
  606.  
  607.   /* restore the globals from the locals */
  608.   g->bb = b;            /* restore global bit buffer */
  609.   g->bk = k;
  610.   return 0;
  611. }
  612.  
  613. LONG inflate_fixed(struct GZipData *g)
  614. /* decompress an inflated type 1 (fixed Huffman codes) block.  We should
  615.    either replace this with a custom decoder, or at least precompute the
  616.    Huffman tables. */
  617. {
  618.   LONG i;        /* temporary variable */
  619.   struct huft *tl;    /* literal/length code table */
  620.   struct huft *td;    /* distance code table */
  621.   LONG bl;        /* lookup bits for tl */
  622.   LONG bd;        /* lookup bits for td */
  623.   ULONG *l;         /* length list for huft_build, we need 288 ULONG's */
  624.  
  625.   l = g->data1;
  626.  
  627.   /* set up literal table */
  628.   for (i = 0; i < 144; i++)
  629.     l[i] = 8;
  630.   for (; i < 256; i++)
  631.     l[i] = 9;
  632.   for (; i < 280; i++)
  633.     l[i] = 7;
  634.   for (; i < 288; i++)        /* make a complete, but wrong code set */
  635.     l[i] = 8;
  636.   bl = 7;
  637.   if((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl,g)) != 0)
  638.     return i;
  639.  
  640.   /* set up distance table */
  641.   for (i = 0; i < 30; i++)    /* make an incomplete code set */
  642.     l[i] = 5;
  643.   bd = 5;
  644.   if((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd,g)) > 1)
  645.   {
  646.     huft_free(tl,g);
  647.     return i;
  648.   }
  649.  
  650.   /* decompress until an end-of-block code */
  651.   i = inflate_codes(tl, td, bl, bd,g);
  652.  
  653.   /* free the decoding tables, return */
  654.   huft_free(tl,g);
  655.   huft_free(td,g);
  656.   return i;
  657. }
  658.  
  659. LONG inflate_dynamic(struct GZipData *g)
  660. /* decompress an inflated type 2 (dynamic Huffman codes) block. */
  661. {
  662.   LONG i;        /* temporary variables */
  663.   ULONG j;
  664.   ULONG l;        /* last length */
  665.   ULONG m;        /* mask for bit lengths table */
  666.   ULONG n;        /* number of lengths to get */
  667.   struct huft *tl;    /* literal/length code table */
  668.   struct huft *td;    /* distance code table */
  669.   LONG bl;        /* lookup bits for tl */
  670.   LONG bd;        /* lookup bits for td */
  671.   ULONG nb;        /* number of bit length codes */
  672.   ULONG nl;        /* number of literal/length codes */
  673.   ULONG nd;        /* number of distance codes */
  674.   ULONG *ll;        /* literal/length and distance code lengths, we need 286+30 ULONG's */
  675.   ULONG b;        /* bit buffer */
  676.   ULONG k;        /* number of bits in bit buffer */
  677.   LONG err;
  678.  
  679.   ll = g->data1;
  680.  
  681.   /* make local bit buffer */
  682.   b = g->bb;
  683.   k = g->bk;
  684.  
  685.   /* read in table lengths */
  686.   NEEDBITS(5)
  687.   nl = 257 + ((ULONG)b & 0x1f); /* number of literal/length codes */
  688.   DUMPBITS(5)
  689.   NEEDBITS(5)
  690.   nd = 1 + ((ULONG)b & 0x1f);    /* number of distance codes */
  691.   DUMPBITS(5)
  692.   NEEDBITS(4)
  693.   nb = 4 + ((ULONG)b & 0xf);    /* number of bit length codes */
  694.   DUMPBITS(4)
  695.   if (nl > 286 || nd > 30)
  696.     return 1;            /* bad lengths */
  697.  
  698.   /* read in bit-length-code lengths */
  699.   for (j = 0; j < nb; j++)
  700.   {
  701.     NEEDBITS(3)
  702.     ll[border[j]] = (ULONG)b & 7;
  703.     DUMPBITS(3)
  704.   }
  705.   for (; j < 19; j++)
  706.     ll[border[j]] = 0;
  707.  
  708.   /* build decoding table for trees--single level, 7 bit lookup */
  709.   bl = 7;
  710.   if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl,g)) != 0)
  711.   {
  712.     if (i == 1)
  713.       huft_free(tl,g);
  714.     return i;            /* incomplete code set */
  715.   }
  716.  
  717.   /* read in literal and distance code lengths */
  718.   n = nl + nd;
  719.   m = mask_bits[bl];
  720.   i = l = 0;
  721.   err = 0;
  722.   while ((ULONG)i < n)
  723.   {
  724.     NEEDBITS((ULONG)bl)
  725.     j = (td = tl + ((ULONG)b & m))->b;
  726.     DUMPBITS(j)
  727.     j = td->v.n;
  728.     if (j < 16)         /* length of code in bits (0..15) */
  729.       ll[i++] = l = j;        /* save last length in l */
  730.     else if (j == 16)        /* repeat last length 3 to 6 times */
  731.     {
  732.       NEEDBITS(2)
  733.       j = 3 + ((ULONG)b & 3);
  734.       DUMPBITS(2)
  735.       if ((ULONG)i + j > n)
  736.       {
  737.         err = 1; break;
  738.       }
  739.       while (j--)
  740.     ll[i++] = l;
  741.     }
  742.     else if (j == 17)        /* 3 to 10 zero length codes */
  743.     {
  744.       NEEDBITS(3)
  745.       j = 3 + ((ULONG)b & 7);
  746.       DUMPBITS(3)
  747.       if((ULONG)i + j > n)
  748.       {
  749.         err = 1; break;
  750.       }
  751.       while (j--)
  752.     ll[i++] = 0;
  753.       l = 0;
  754.     }
  755.     else            /* j == 18: 11 to 138 zero length codes */
  756.     {
  757.       NEEDBITS(7)
  758.       j = 11 + ((ULONG)b & 0x7f);
  759.       DUMPBITS(7)
  760.       if((ULONG)i + j > n)
  761.       {
  762.         err = 1; break;
  763.       }
  764.       while (j--)
  765.     ll[i++] = 0;
  766.       l = 0;
  767.     }
  768.   }
  769.  
  770.   /* free decoding table for trees */
  771.   huft_free(tl,g);
  772.  
  773.   if(err)
  774.     return err;
  775.  
  776.   /* restore the global bit buffer */
  777.   g->bb = b;
  778.   g->bk = k;
  779.  
  780.   /* build the decoding tables for literal/length and distance codes */
  781.   bl = lbits;
  782.   if((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl,g)) != 0)
  783.   {
  784.     if(i == 1)
  785.       huft_free(tl,g);
  786.     return i;            /* incomplete code set */
  787.   }
  788.   bd = dbits;
  789.   if((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd,g)) != 0)
  790.   {
  791.     if(i == 1)
  792.       huft_free(td,g);
  793.     huft_free(tl,g);
  794.     return i;            /* incomplete code set */
  795.   }
  796.  
  797.   /* decompress until an end-of-block code */
  798.   err = inflate_codes(tl, td, bl, bd, g);
  799.  
  800.   /* free the decoding tables, return */
  801.   huft_free(tl,g);
  802.   huft_free(td,g);
  803.   return err;
  804. }
  805.  
  806. BOOL decrunchZip(struct GZipData *g)
  807. {
  808.   LONG e;    /* last block flag */
  809.   LONG t;    /* block type */
  810.   ULONG b;    /* bit buffer */
  811.   ULONG k;    /* number of bits in bit buffer */
  812.  
  813.   g->XFDerr = XFDERR_CORRUPTEDDATA;
  814.   /* decompress until the last block */
  815.   do
  816.   {
  817.     b = g->bb; k = g->bk;
  818.     NEEDBITS(1) e = (LONG)b & 1; DUMPBITS(1)
  819.     NEEDBITS(2) t = (LONG)b & 3; DUMPBITS(2)
  820.     g->bb = b; g->bk = k;
  821.  
  822.     /* inflate that block type */
  823.     switch(t)
  824.     {
  825.     case 0: t = inflate_stored(g); break;
  826.     case 1: t = inflate_fixed(g); break;
  827.     case 2: t = inflate_dynamic(g); break;
  828.     };
  829.  
  830.     if(t)
  831.     {
  832.       if(t == 300)
  833.         g->XFDerr = XFDERR_BUFFERTRUNCATED;
  834.       else if(t == 100)
  835.     g->XFDerr = XFDERR_NOMEMORY;
  836.       return 0;
  837.     }
  838.   } while(!e);
  839.  
  840.   /* calculate crc value */
  841.   {
  842.     ULONG *buf; /* 256 entries */
  843.     STRPTR p;
  844.  
  845.     buf = g->data1;
  846.  
  847.     for(e = 0; e < 256; ++e)
  848.     {
  849.       k = 2*e;
  850.       for(t = b = 0; t < 8; ++t)
  851.       {
  852.         k >>= 1;
  853.         b = ((k ^ b) & 1) ? (b >> 1) ^ 0xEDB88320 : b >> 1;
  854.       }
  855.       buf[e] = b;
  856.     }
  857.  
  858.     b = 0xFFFFFFFF;
  859.     e = g->outsize-BUFFEREXTEND;
  860.     p = g->outbuf;
  861.   
  862.     while(e--)
  863.       b = buf[(b ^ *(p++)) & 0xFF] ^ (b >> 8);
  864.   }
  865.  
  866.   if(~b == g->crc)
  867.     return 1;
  868.   else
  869.     return 0;
  870. }
  871.  
  872.